1. What is a union in C?
Correct Answer: (A) A user-defined data type that stores different types of data in the same memory location.
2. How much memory does a union occupy?
Correct Answer: (B) Equal to the size of the largest member.
3. Which keyword is used to declare a union in C?
Correct Answer: (B) union.
4. How are union members accessed in C?
Correct Answer: (C) Using the dot (.) operator.
5. Which of the following is a correct way to declare a union?
Correct Answer: (B) union example { int a; float b; }.
6. What happens when you assign a value to a member of a union?
Correct Answer: (B) Only the last assigned member holds a valid value.
7. How does the memory allocation for a union differ from that of a structure?
Correct Answer: (C) Union allocates memory for only one member at a time.
8. Which is true regarding unions in C?
Correct Answer: (B) Only one member of a union can store a value at a time.
9. What is the size of the union below if char is 1 byte, int is 4 bytes, and float is 4 bytes?
union example {
char ch;
int a;
float b;
};
Correct Answer: (D) 4 bytes.
10. What is the primary advantage of using a union over a structure?
Correct Answer: (A) Reduced memory usage.
11. How can you declare a variable for a union named data in C?
Correct Answer: (A) union data d;
12. What will be the output of the following code if char is 1 byte and int is 4 bytes?
union example {
char ch;
int a;
};
union example ex;
printf("%lu", sizeof(ex));
Correct Answer: (B) 4
13. Which of the following is a valid difference between unions and structures?
Correct Answer: (A) Union members share the same memory, while structure members do not
14. Can a union store multiple values simultaneously?
Correct Answer: (B) No
15. Which statement is true about the following union?
union myUnion {
int a;
float b;
};
Correct Answer: (B) myUnion can store either an int or a float, but not both at the same time
16. Which is the correct syntax to access a union member?
Correct Answer: (A) union_name.member_name
17. What is the size of the following union?
union myUnion {
double d;
char ch[5];
};
Correct Answer: (B) 8 bytes
18. In a union, which member is initialized last in memory?
Correct Answer: (B) The member assigned the last value
19. What is the correct output of the following code?
union example {
int a;
float b;
};
union example ex;
ex.a = 10;
ex.b = 3.14;
printf("%d", ex.a);
Correct Answer: (C) Undefined behavior
20. Which of the following operations is NOT valid for unions in C?
Correct Answer: (D) Storing multiple members simultaneously
21. What is a nested union in C?
Correct Answer: (B) A union declared inside another union
22. Which keyword is used to declare a nested union?
Correct Answer: (A) union
23. In a nested union, which memory size is allocated for the union?
Correct Answer: (B) The size of the largest member in the entire hierarchy
24. What is the benefit of using nested unions in C?
Correct Answer: (B) Improves memory efficiency and organizes related data
25. Which of the following is a valid way to access a member of a nested union?
Correct Answer: (A) outerUnion.innerUnion.member
26. In the following code, what is the type of myUnion.innerUnion.innerChar?
union OuterUnion {
int outerInt;
union InnerUnion {
char innerChar;
double innerDouble;
} innerUnion;
};
Correct Answer: (B) char
27. What is a key difference between structures and unions in C?
Correct Answer: (B) Structures allocate memory for each member, unions allocate memory for the largest member
28. How are members of a union accessed?
Correct Answer: (A) Using the dot (.) operator
29. What happens if you assign a value to the outerInt and then access innerChar in the following union?
union OuterUnion {
int outerInt;
union InnerUnion {
char innerChar;
double innerDouble;
} innerUnion;
};
Correct Answer: (D) Undefined behavior
30. Which of the following demonstrates the correct way to initialize a nested union?
Correct Answer: (B) union OuterUnion myUnion = {.outerInt = 10};
31. In the given code, which member of the outer union takes up the most memory?
union OuterUnion {
int outerInt;
union InnerUnion {
char innerChar;
double innerDouble;
} innerUnion;
};
Correct Answer: (C) innerDouble.
32. Which of the following is an advantage of using unions in C?
Correct Answer: (B) It improves memory efficiency by sharing memory among members.
33. How many members of a nested union can be active at the same time?
Correct Answer: (C) Only one member at a time from either the inner or outer union.
34. In the following example, what will the size of OuterUnion be?
union OuterUnion {
int outerInt;
float outerFloat;
union InnerUnion {
char innerChar;
double innerDouble;
} innerUnion;
};
Correct Answer: (C) 8 bytes.
35. Which member of the nested union will be accessed in this code?
union OuterUnion myUnion;
myUnion.innerUnion.innerChar = 'B';
printf("%c", myUnion.innerUnion.innerChar);
Correct Answer: (C) innerChar.
36. What happens to the value of the outer union when you assign a value to a member of the inner union?
Correct Answer: (B) The outer union's value is lost.
37. In the following code, which member is part of the inner union?
union OuterUnion {
int outerInt;
union InnerUnion {
char innerChar;
double innerDouble;
} innerUnion;
};
Correct Answer: (B) innerChar.
38. What does the following code print?
union OuterUnion {
int outerInt;
union InnerUnion {
char innerChar;
double innerDouble;
} innerUnion;
};
union OuterUnion myUnion;
myUnion.innerUnion.innerDouble = 3.14;
printf("%f", myUnion.innerUnion.innerDouble);
Correct Answer: (A) 3.14.
39. How is memory allocated when a union is nested within another union?
Correct Answer: (B) The largest member of both unions determines the total memory.
40. Can a union be nested inside a structure in C?
Correct Answer: (A) Yes.
41. What is an array of unions in C?
Correct Answer: (B) An array where each element is a union that can hold different data types.
42. How is an array of unions declared in C?
Correct Answer: (A) union MyUnion array[5];.
43. In an array of unions, each element in the array:
Correct Answer: (C) Can store one of several data types defined in the union.
44. What is the memory size allocated for each element in an array of unions?
Correct Answer: (B) The size of the largest member in the union.
45. Which of the following operations is valid when using an array of unions?
Correct Answer: (B) Assigning values to different members in different array indices.
46. In the following code, what type is stored in arrayOfUnions[1]?
union MyUnion {
int intValue;
float floatValue;
char charValue;
};
union MyUnion arrayOfUnions[5];
arrayOfUnions[1].floatValue = 3.14;
Correct Answer: (B) float.
47. What happens if you assign values to two members of a union at the same index in an array of unions?
Correct Answer: (C) Only the second value is retained.
48. What is the benefit of using arrays of unions?
Correct Answer: (B) They provide memory efficiency by allocating space for only the largest union member.
49. How do you access an element of a union inside an array of unions?
Correct Answer: (A) array[i].unionMember.
50. In the following example, what value will be printed for arrayOfUnions[2].charValue?
union MyUnion {
int intValue;
float floatValue;
char charValue;
};
union MyUnion arrayOfUnions[5];
arrayOfUnions[2].charValue = 'A';
printf("%c", arrayOfUnions[2].charValue);
Correct Answer: (A) A.
51. Can an array of unions contain elements with different data types?
Correct Answer: (A) Yes, each element of the array can store different types of data at different times
52. Which of the following statements is true about arrays of unions?
Correct Answer: (B) Only one member of the union can be used at a time in each element
53. In the following code, what is the output for items[1].price?
union ItemAttributes {
int weight;
float price;
char category;
};
union ItemAttributes items[3];
items[1].price = 19.99;
printf("%.2f", items[1].price);
Correct Answer: (A) 19.99
54. How is memory allocated for arrays of unions?
Correct Answer: (B) Each element uses the memory required by the largest member of the union
55. Can arrays of unions be passed as function arguments?
Correct Answer: (A) Yes, arrays of unions can be passed to functions like any other array
56. What would happen if you try to print an uninitialized element of an array of unions?
Correct Answer: (C) Undefined behavior
57. Which of the following correctly initializes an element in an array of unions?
Correct Answer: (B) array[0].intValue = 10;
58. In an array of unions, each element is initialized:
Correct Answer: (B) Manually for each member
59. Can arrays of unions be dynamically allocated?
Correct Answer: (A) Yes, using malloc
60. What is a key difference between arrays of unions and arrays of structures?
Correct Answer: (B) Arrays of unions allocate memory based on the largest member, arrays of structures allocate memory for all members
61. How can you pass a union to a function in C?
Correct Answer: (D) All of the above
62. What happens when you pass a union by value to a function?
Correct Answer: (B) A copy of the union is passed, and the original remains unchanged
63. What is the syntax to pass a specific member of a union to a function?
Correct Answer: (A) functionName(union.member);
64. Why would you pass a union by reference to a function?
Correct Answer: (D) All of the above
65. Which operator is used to access members of a union when passed as a pointer to a function?
Correct Answer: (C) -> (arrow operator)
66. What is the result of passing a union pointer to a function?
Correct Answer: (B) The original union can be modified
67. How can you ensure type safety when passing union members to a function?
Correct Answer: (B) By specifying the correct data type in the function argument
68. What happens if you pass the wrong union member type to a function?
Correct Answer: (B) Unpredictable behavior
69. How is memory handled when passing a union by value to a function?
Correct Answer: (B) A new memory location is created for the union copy
70. Which of the following is a valid function signature for passing a union by reference?
Correct Answer: (B) void function(union MyUnion *u);
71. When passing a union pointer to a function, how can you modify a member of the union?
Correct Answer: (B) By using the -> (arrow) operator.
72. What is the benefit of passing specific union members rather than the entire union?
Correct Answer: (A) It saves memory and improves performance.
73. Which of the following is true about passing unions by value to a function?
Correct Answer: (B) The original union remains unchanged after the function call.
74. How do you pass a union as an argument to a function if you want to avoid copying the union?
Correct Answer: (D) Both A and B.
75. Which keyword is used to define a union in C?
Correct Answer: (C) union.
76. What is the primary difference between passing a structure and a union to a function?
Correct Answer: (A) A union stores only one value at a time, while a structure stores all its members.
77. How do you pass multiple members of a union to a function at the same time?
Correct Answer: (D) Unions cannot pass multiple members simultaneously.
78. What is a possible drawback of passing union members to a function?
Correct Answer: (B) Loss of type safety.
79. How can you prevent modification of a union passed to a function?
Correct Answer: (C) Pass a constant pointer to the union.
80. What happens when a union is passed by reference to a function?
Correct Answer: (B) The function works directly on the original union.